<?php
require_once $_SERVER['DOCUMENT_ROOT'] . "/vendor/autoload.php";

use src\StringUtil;
use src\util;
use src\JavaUtil;

$table = util::param('table');

$basePackage = util::param('basePackage');
$savePath = util::param('baseSavePath') . '\\' . JavaUtil::$packageController;
$package = $basePackage . '.' . JavaUtil::$packageController;

$entityClass = StringUtil::upper_AndFirst($table);
$serviceClass = JavaUtil::clearEntitySuffix($entityClass) . 'Service';
$serviceVar = lcfirst($serviceClass);
$controllerClass = JavaUtil::clearEntitySuffix($entityClass) . 'Controller';

util::setSavePath($savePath, $controllerClass . '.java');
?>
package <?= $package ?>;

import <?= $basePackage . '.'.JavaUtil::$packageService.'.' . $serviceClass ?>;
import <?= $basePackage . '.'.JavaUtil::$packageEntity.'.' . $entityClass ?>;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@Controller
@RequestMapping("/api/<?=$serviceVar?>")
public class <?= $controllerClass ?> extends BaseController{

    @Autowired
    private <?= $serviceClass ?> service;

    @RequestMapping("/create")
    @ResponseBody
    public Map<String, Object> create(HttpServletRequest request, <?=$entityClass?> data) {
        UserSesionBean userSessionBean = IuvSessionUtil.getUserSessionBean(request);
        service.checkRepeat(data);
        data.setId(null);
        data.setUserId(userSessionBean.getUserId());
        service.repository.save(data);
        return result(null);
    }

}
